// Description
// -----------
// Reads the input from the debounce circuit and parses out the button action.
// it returns a 4-bit code
// 
// Input assignment:
// pbtn_db - {BTN_LEFT, BTN_UP, BTN_RIGHT, BTN_DOWN, BTN_CENTER}
//      BTN_LEFT  = 4'b10000
//      BTN_UP    = 4'b01000
//      BTN_RIGHT = 4'b00100
//      BTN_DOWN  = 4'b00010
//
// Output assignment:
// * decode - 
//            - bit[0] - '0' = move forward.     | '1' = move reverse
//            - bit[1] - '0' = no rotation.      | '1' = rotation
//            - bit[2] - '0' = CW rotation.      | '1' = CCW rotation 
//            - bit[3] - '0' = 1x rotation speed | '1' = 2x rotation speed  
//

`timescale  1 ns / 1 ns
module ButtonPressDecoder (
    
    //inputs
    input               clk,
    input       [3:0]   pbtn_db,                // pushbutton debounced inputs

    //outputs
    output reg  [2:0]   decode = 3'hf           // decode output
);

    localparam BTN_LEFT_FWD = 4'b1000; 
    localparam BTN_LEFT_REV = 4'b0100;
    localparam BTN_RGHT_FWD = 4'b0010;
    localparam BTN_RGHT_REV = 4'b0001;
    localparam BTN_CENTER   = 4'b0000;

    localparam stopMotor   = 4'b1111;
    localparam movFWD      = BTN_LEFT_FWD | BTN_RGHT_FWD;
    localparam movREV      = BTN_LEFT_REV | BTN_RGHT_REV;
    localparam rotCW_1x_1  = BTN_LEFT_FWD; // CW rot 1X = button left forward
    localparam rotCW_1x_2  = BTN_RGHT_REV; // CW rot 1X = button right reverse
    localparam rotCW_2x    = BTN_LEFT_FWD | BTN_RGHT_REV; // CW rot 2X = button right reverse + left forward
    localparam rotCCW_1x_1 = BTN_RGHT_FWD; // CCW rot 1X = button right forward
    localparam rotCCW_1x_2 = BTN_LEFT_REV; // CCW rot 1X = button left reverse
    localparam rotCCW_2x   = BTN_LEFT_REV | BTN_RGHT_FWD; 

    always @(posedge clk) begin
        case (pbtn_db)
            stopMotor   : decode = 3'b111; // all '1' for stop motor
            movFWD      : decode = 3'd1;   // b[0]='0' for forward
            movREV      : decode = 3'd2;   // b[0]='1' for reverse
            rotCW_2x    : decode = 3'd3;   // b[1]='1' for rot, b[3]='1' for 2x 
            rotCCW_2x   : decode = 3'd4;
            rotCW_1x_1  : decode = 3'd5; 
            rotCW_1x_2  : decode = 3'd5;
            rotCCW_1x_1 : decode = 3'd6;
            rotCCW_1x_2 : decode = 3'd6;
            default     : decode = 3'b111; // Stop motor
        endcase 
    end
endmodule